﻿using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TestApp.Model.Data;

namespace TestApp.WebService.Parsers
{
    public class StoresParser : Parser
    {
        // Constant for the Stores TAG
        private static String STORES_TAG = "Stores";
        // Constant for the ChainId TAG
        private static String CHAIN_ID_TAG = "ChainId";
        // Constant for the StoreId TAG
        private static String STORE_ID_TAG = "StoreId";
        // Constant for the Name TAG
        private static String NAME_TAG = "Name";
        // Constant for the Schedule TAG
        private static String SCHEDULE_TAG = "Schedule";
        // Constant for the Address TAG
        private static String ADDRESS_TAG = "Address";
        // Constant for the Address1 TAG
        private static String ADDRESS_1_TAG = "Address1";
        // Constant for the City TAG
        private static String CITY_TAG = "City";
        // Constant for the State TAG
        private static String STATE_TAG = "State";
        // Constant for the PostalCode TAG
        private static String POSTAL_CODE_TAG = "PostalCode";
        // Constant for the Phone TAG
        private static String PHONE_TAG = "Phone";
        // Constant for the Email TAG
        private static String EMAIL_TAG = @"Email";
        // Constant for the Distance TAG
        private static String DISTANCE_TAG = "Distance";
        // Constant for the latitude TAG
        private static String LATITUDE_TAG = "Latitude";
        // Constant for the Longitude TAG
        private static String LONGITUDE_TAG = "Longitude";
        // Constant for the MwgServices TAG
        private static String MWG_SERVICES_TAG = "MwgServices";
        // Constant for the Uri TAG
        private static String URI_TAG = "Uri";
        // Constant for the PseudoStoreId TAG
        private static String PSEUDO_STORE_ID_TAG = "PseudoStoreId";
        // Constant for the ServiceName TAG
        private static String SERVICE_NAME_TAG = "ServiceName";

        public override object ParseResults(WSResponse results)
        {
            // JSONObject
            JObject jsonData = JObject.Parse(results.Data);

            // JSONStores
            JArray jsonStores = (JArray)jsonData[STORES_TAG];

            // Stores List
            List<Store> stores = new List<Store>();
            foreach (JObject jsonStore in jsonStores)
            {
                Store store = new Store();
                
                // Store Name
                store.Name = (String)jsonStore[NAME_TAG];

                // Store ChainId
                store.ChainId = (Int32)jsonStore[CHAIN_ID_TAG];

                // Store Email
                store.Email = (String)jsonStore[EMAIL_TAG];

                // Store Longitude
                store.Longitude = (Double)jsonStore[LONGITUDE_TAG];

                // Store Latitude
                store.Latitude = (Double)jsonStore[LATITUDE_TAG];

                // Store Schedule
                store.Schedule = (String)jsonStore[SCHEDULE_TAG];

                // Store Id
                store.StoreId = (Int32)jsonStore[STORE_ID_TAG];

                // Store PseudoStore Id
                store.PseudoStore = (String)jsonStore[PSEUDO_STORE_ID_TAG];

                // Store Address
                JObject jsonAddress = (JObject)jsonStore[ADDRESS_TAG];

                store.Address = (String)jsonAddress[ADDRESS_1_TAG];
                store.City = (String)jsonAddress[CITY_TAG];
                store.PostalCode = (String)jsonAddress[POSTAL_CODE_TAG];
                store.State = (String)jsonAddress[STATE_TAG];

                // MWG Services
                JArray jsonMwgServices = (JArray)jsonStore[MWG_SERVICES_TAG];
                store.MWGServices = new List<MwgService>();

                foreach(JObject jsonMWGService in jsonMwgServices) {
                    MwgService service = new MwgService();
                    service.Name = (String)jsonMWGService[SERVICE_NAME_TAG];
                    service.Uri = (String)jsonMWGService[URI_TAG];
                    store.MWGServices.Add(service);
                }
                stores.Add(store);

            }
            return stores;
        }
    }
}
